﻿using Business.Base;
using Business.Base.Common;
using Business.Base.Enums;
using Business.Base.Model;
using Business.Base.Service;
using Business.ScrewPump.Perf;

using Configuration;
using System;
using System.Collections.Generic;

using System.Windows.Forms;

namespace Pumptester.ExperimentForms.ScrewPump.Perf
{
    public partial class CreateForm : Pumptester.ExperimentForms.Base.BaseCreateForm
    {
        const ExperimentType experimentType = ExperimentType.ScrewPumpPerf;
       // const PumpType pumpType = PumpType.ScrewPump;
     
        protected override void BandCombox()
        {
            Dictionary<ComboBox, DataSource[]> boxDic = new Dictionary<ComboBox, DataSource[]>();

            boxDic.Add(comboBoxFlowmeter, DataSource.DataSourceDic[DataSource.FLOWMETER]);

            boxDic.Add(comboBoxWindingConnection, DataSource.DataSourceDic[DataSource.WINDINGCONNECTION]);
            boxDic.Add(comboBoxWindingMaterial, DataSource.DataSourceDic[DataSource.WINGDINGMATERIAL]);
            boxDic.Add(comboBoxLevel, DataSource.DataSourceDic[DataSource.PRESUREFLOWLEVEL]);
            boxDic.Add(comboBoxRateVoltage, DataSource.DataSourceDic[DataSource.MOTORRATEDVOLTAGE]);


            foreach (KeyValuePair<ComboBox, DataSource[]> pair in boxDic)
            {
                pair.Key.DataSource = pair.Value;
                pair.Key.DisplayMember = "BindMember";
                pair.Key.ValueMember = "BindValue";
                pair.Key.SelectedIndex = 0;
            }
            //this.comboBoxPumpType.SelectedValue = pumpType;
        }

        protected override void initValidateInfo()
        {
            notNullList = new List<TextBox>();
            doubleFormateList = new List<TextBox>();
            doubleorNullFormateList = new List<TextBox>();

            intList = new List<TextBox>();

            notNullList.Add(this.textBoxCode);
            notNullList.Add(this.textBoxPumpName);
            notNullList.Add(this.textBoxPumpCode);
            notNullList.Add(this.textBoxPumpCode);
            notNullList.Add(this.textBoxExperimentOwner);
            notNullList.Add(this.textBoxManufacturer);
            notNullList.Add(this.textBoxRatedFlow);
            notNullList.Add(this.textBoxRatedPresure);
            notNullList.Add(this.textBoxRatedPower);
            notNullList.Add(this.textBoxDInlet);
            notNullList.Add(this.textBoxDOutlet);
            notNullList.Add(this.textBoxImpellerDia);
            notNullList.Add(this.textBoxRatedSpeed);
            
            //notNullList.Add(this.textBoxRatedVoltage);
            notNullList.Add(this.textBoxRatedCurrent);
            //notNullList.Add(this.textBoxCurrentTransRatio);
            notNullList.Add(this.textBoxMotorEff);
            notNullList.Add(this.textBoxZInLet);
            notNullList.Add(this.textBoxZOutLet);

            doubleFormateList.Add(this.textBoxRatedFlow);
            doubleFormateList.Add(this.textBoxRatedPresure);
            doubleFormateList.Add(this.textBoxRatedPower);
            doubleFormateList.Add(this.textBoxDInlet);
            doubleFormateList.Add(this.textBoxDOutlet);
            doubleFormateList.Add(this.textBoxRatedSpeed);
            doubleFormateList.Add(this.textBoxImpellerDia);
            //doubleFormateList.Add(this.textBoxRatedVoltage);
            doubleFormateList.Add(this.textBoxRatedCurrent);

            doubleFormateList.Add(this.textBoxZInLet);
            doubleFormateList.Add(this.textBoxZOutLet);

            doubleFormateList.Add(this.textBoxMotorEff);
            doubleFormateList.Add(this.textBoxZInLet);
            doubleFormateList.Add(this.textBoxZOutLet);

           // doubleorNullFormateList.Add(this.textBoxBenchmarkWorkingTemperature);
            //doubleorNullFormateList.Add(this.textBoxStrayLossRate);

            intList.Add(this.textBoxImpellerDia);
            //intList.Add(this.textBoxCurrentTransRatio);


            //Xyj 添加验证逻辑
            //floatValidateList.Add();

        }

        protected override Experiment buildExperiment()
        {
            ScrewPumpPerfExperiment experiment = new ScrewPumpPerfExperiment();

            experiment.ExperimentCode = this.textBoxCode.Text;
            experiment.PumpName = this.textBoxPumpName.Text;
            experiment.PumpModel = this.textBoxProductModel.Text;
            experiment.PumpCode = this.textBoxPumpCode.Text;
            experiment.ExperimentOwner = this.textBoxExperimentOwner.Text;
            experiment.Manufacturer = this.textBoxManufacturer.Text;
            //experiment.PumpType = pumpType;//螺杆泵
            experiment.Customer = this.textBoxCustomer.Text;

            experiment.PumpRatedFlow = Convert.ToDouble(this.textBoxRatedFlow.Text);
            experiment.PumpRatedPresure  = Convert.ToDouble(this.textBoxRatedPresure.Text);
            experiment.PumpRatedPower  = Convert.ToDouble(this.textBoxRatedPower.Text);
            experiment.PumpRatedEfficiency = Convert.ToDouble(this.textBoxRatedEff.Text);

            //experiment.DesignStages = (int)Convert.ToInt32(this.textBoxStages.Text);
            experiment.MotorRatedSpeed = Convert.ToDouble(this.textBoxRatedSpeed.Text);
            //experiment.DesignSynchronousSpeed = Convert.ToDouble(this.textBoxSynchronousSpeed.Text);
            experiment.ImpellerDia = Convert.ToDouble(this.textBoxImpellerDia.Text);
            experiment.MotorRatedVoltage = (MotorRatedVoltage)Convert.ToInt32(this.comboBoxRateVoltage.SelectedValue);
            experiment.MotorRatedCurrent = Convert.ToDouble(this.textBoxRatedCurrent.Text);
            experiment.MotorRatedEfficiency  = Convert.ToDouble(this.textBoxMotorEff.Text);
            experiment.MotorWindingConnection = (string)this.comboBoxWindingConnection.SelectedValue;
            experiment.MotorWindingMaterial = (String)this.comboBoxWindingMaterial.SelectedValue;
            //experiment.DesignBenchmarkWorkingTemprature = Convert.ToDouble(this.textBoxBenchmarkWorkingTemperature.Text);
            //experiment.DesignStrayLoss = Convert.ToDouble(this.textBoxStrayLossRate.Text);
            //experiment.CurrentTransformationRatio = Convert.ToDouble(this.textBoxCurrentTransRatio.Text);
            experiment.MotorRatedEfficiency  = Convert.ToDouble(this.textBoxMotorEff.Text);

            experiment.DInlet = Convert.ToDouble(this.textBoxDInlet.Text)/1000;
            experiment.DOutlet = Convert.ToDouble(this.textBoxDOutlet.Text)/1000;

            experiment.ZInlet = Convert.ToDouble(this.textBoxZInLet.Text)/1000 ;
            experiment.ZOutlet = Convert.ToDouble(this.textBoxZOutLet.Text)/1000;

            experiment.Flowmeter = comboBoxFlowmeter.Text;
            experiment.FlowmeterAddress = (int)comboBoxFlowmeter.SelectedValue;
            //experiment.ExperimentType = ExperimentType.Perf;
            experiment.ExperimentLevel = (PresureFlowLevel)this.comboBoxLevel.SelectedValue;
            experiment.CreateTime = DateTime.Now;
            experiment.ExperimentType = ExperimentType.ScrewPumpPerf;
            //experiment.TransformationRatio = double.Parse(this.comboBoxCurrentTransRatio.SelectedValue.ToString());
            experiment.TransformationRatio = Convert.ToDouble(this.textBoxCurrentTransRatio.Text);
            //experiment.TransFormRadio = Convert.ToDouble(this.textBoxCurrentTransRatio.se);

            return experiment;
            //Experiment = experiment;
        }
        public CreateForm()
        {

            InitializeComponent();
            this.textBoxCode.Text = ExperimentService.GenerateExperimentCode(experimentType);
        }


        protected override String RecentRecordFilePath => Config.BasePath + "\\" + experimentType  + "-recent.obj";

        protected override void assignRecentRecordToFace(Experiment experiment)
        {
            ScrewPumpPerfExperiment screwPumpPerfExperiment = (ScrewPumpPerfExperiment)experiment;

            this.textBoxPumpName.Text = screwPumpPerfExperiment.PumpName;

            this.textBoxProductModel.Text = screwPumpPerfExperiment.PumpModel;
            this.textBoxPumpCode.Text = screwPumpPerfExperiment.PumpCode;
            this.textBoxExperimentOwner.Text = screwPumpPerfExperiment.ExperimentOwner;
            this.textBoxManufacturer.Text = screwPumpPerfExperiment.Manufacturer;


            this.textBoxRatedFlow.Text = screwPumpPerfExperiment.PumpRatedFlow.ToString();
            this.textBoxRatedPresure.Text = screwPumpPerfExperiment.PumpRatedPresure.ToString();
            this.textBoxRatedPower.Text = screwPumpPerfExperiment.PumpRatedPower.ToString();
            this.textBoxRatedEff.Text = screwPumpPerfExperiment.PumpRatedEfficiency.ToString();

            this.textBoxRatedSpeed.Text = screwPumpPerfExperiment.MotorRatedSpeed.ToString();
 
            this.textBoxImpellerDia.Text = screwPumpPerfExperiment.ImpellerDia.ToString();

            this.comboBoxRateVoltage.SelectedValue = (int)screwPumpPerfExperiment.MotorRatedVoltage;
            this.textBoxRatedCurrent.Text = screwPumpPerfExperiment.MotorRatedCurrent.ToString();
        
            this.comboBoxWindingConnection.SelectedValue = screwPumpPerfExperiment.MotorWindingConnection;
            this.comboBoxWindingMaterial.SelectedValue = screwPumpPerfExperiment.MotorWindingMaterial;
            this.textBoxMotorEff.Text = screwPumpPerfExperiment.MotorRatedEfficiency.ToString();

            this.textBoxDInlet.Text = (screwPumpPerfExperiment.DInlet*1000).ToString();
            this.textBoxDOutlet.Text = (screwPumpPerfExperiment.DOutlet*1000).ToString();

            this.textBoxZInLet.Text = (screwPumpPerfExperiment.ZInlet*1000).ToString();
            this.textBoxZOutLet.Text = (screwPumpPerfExperiment.ZOutlet*1000).ToString();

            comboBoxFlowmeter.Text = screwPumpPerfExperiment.Flowmeter;
            comboBoxFlowmeter.SelectedValue = screwPumpPerfExperiment.FlowmeterAddress;
            this.comboBoxLevel.SelectedValue = screwPumpPerfExperiment.ExperimentLevel;

            //this.comboBoxFlowmeter.SelectedValue = screwPumpPerfExperiment.FlowmeterAddress;
            //this.comboBoxCurrentTransRatio.SelectedValue = screwPumpPerfExperiment.TransformationRatio.ToString();
            this.textBoxCurrentTransRatio.Text = screwPumpPerfExperiment.TransformationRatio.ToString();

            //this.textBoxCurrentTransRatio.Text = screwPumpPerfExperiment.TransFormRadio.ToString();

            this.textBoxCustomer.Text = screwPumpPerfExperiment.Customer;

        }

        private void changeRatedCurrent()
        {
            try
            {
                double designedPower = Convert.ToDouble(this.textBoxRatedPower.Text);
                double ratedVoltage = Convert.ToDouble(this.comboBoxRateVoltage.SelectedValue);
                double transRadio = Config.TransFormationRadioConfig[ratedVoltage, designedPower];
                if (!Double.IsNaN(transRadio))
                {
                    this.textBoxCurrentTransRatio.Text = transRadio.ToString();
                }
                else
                {
                    this.textBoxCurrentTransRatio.Text = "";
                }
            }
            catch (FormatException ex)
            {
                ex.GetType();
                Console.WriteLine(ex.ToString());
            }
        }

        private void btnSure_Click(object sender, EventArgs e)
        {

        }

        private void textBoxRatedPower_Leave(object sender, EventArgs e)
        {
            changeRatedCurrent();
        }

        private void comboBoxRateVoltage_SelectedIndexChanged(object sender, EventArgs e)
        {
            changeRatedCurrent();
        }
    }
}
